package cn.blog.controller;

import cn.blog.entity.Blogger;
import cn.blog.service.BloggerService;
import cn.blog.util.CryptographyUtil;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.Map;

@Controller
@RequestMapping("/blogger")
public class BloggerController {

    @Resource
    BloggerService bloggerService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(Blogger blogger, HttpServletRequest request,String code,HttpSession session){
        Map<String,Object> map = new LinkedHashMap<>();
        String userName =blogger.getUserName();
        String password = blogger.getPassword();
        String pw = CryptographyUtil.md5(password);
        //Subject
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userName,pw);
        //传递token给shiro的realm
        try {
            subject.login(token);
            map.put("msg","ok");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg","no");
            map.put("blogg",blogger);
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/isphone")
    @ResponseBody
    public String index03(String phone, HttpSession session){
        System.out.println("发送验证码");
        System.out.println(phone);
        Map<String,Object> map = new LinkedHashMap<>();
        int code = (int) ((Math.random() * 9 + 1) * 100000);
        session.setAttribute("code",code);
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FhNBFMo5NhUiyLiQzCP", "78bKjxOdg8GtRB6yCBkjTn1VlLzeF9");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "君忆zm");
        request.putQueryParameter("TemplateCode", "SMS_175534964");
        request.putQueryParameter("TemplateParam",  "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            map.put("msg","ok");
        } catch (ServerException e) {
            e.printStackTrace();
            map.put("msg",e.getMessage());
        } catch (ClientException e) {
            e.printStackTrace();
            map.put("msg",e.getMessage());
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/isyes")
    @ResponseBody
    public String logins(String code,HttpSession session){
        System.out.println("验证验证码存不存在");
        System.out.println(session.getAttribute("code").toString());
        Map<String,Object> map = new LinkedHashMap<>();
        String code2 = session.getAttribute("code").toString();
        if(code2.equals(code)){
            map.put("msg","ok");
        }else{
            map.put("msg","验证码不存在");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("add")
    @ResponseBody
    public String add(String adduserName,String addpassword,String addphone,String code,HttpSession session){
        Map<String,Object> map = new LinkedHashMap<>();
        Blogger blogger = new Blogger(adduserName,CryptographyUtil.md5(addpassword), addphone);
        Integer insert = bloggerService.insert(blogger);
        if(insert > 0){
            map.put("msg","ok");
        }else{
            map.put("msg","no");
            map.put("blogg",blogger);
        }
        return JSON.toJSONString(map);
    }
}
